clear all


global fontsize_axis vlarge
global fontsize_title vlarge

set scheme s1mono

cd "D:\Dropbox\DynamicDI\3. Data\HRS_RAND"

use randhrs1992_2018v1.dta, clear

cd "D:\Dropbox\DynamicDI\replication_final\eventstudy"

ren hhidpn id
*ren h?hhid hhid?
*ren h??hhid hhid??

ren s?hhidpn sp_id?
ren s??hhidpn sp_id??

ren r?iwstat response?
ren r??iwstat response??

ren s?iwstat sp_response?
ren s??iwstat sp_response??

ren h?hhresp ncore?
ren h??hhresp ncore??
 
ren h?cpl couple?
ren h??cpl couple??

ren h?child numkids?
ren h??child numkids??

ren ravetrn veteran 

ren h?ahous homevalue?
ren h??ahous homevalue??


ren racohbyr cohort

ren s?cohbyr sp_cohort?
ren s??cohbyr sp_cohort??

ren rabyear byear
ren rabmonth bmonth
ren rabdate bdate

ren s?byear sp_byear?
ren s?bmonth sp_bmonth?
ren s?bdate sp_bdate?
ren s??byear sp_byear??
ren s??bmonth sp_bmonth??
ren s??bdate sp_bdate??

ren radyear dyear
ren radmonth dmonth
ren raddate ddate

ren s?dyear sp_dyear?
ren s?dmonth sp_dmonth?
ren s?ddate sp_ddate?
ren s??dyear sp_dyear??
ren s??dmonth sp_dmonth??
ren s??ddate sp_ddate??

ren r?agem_b agemonth?
ren r??agem_b agemonth??
ren r?agey_b age?
ren r??agey_b age??

ren s?agem_b sp_agemonth?
ren s??agem_b sp_agemonth??
ren s?agey_b sp_age?
ren s??agey_b sp_age??

ren r?iwbeg int_stdate?
ren r??iwbeg int_stdate??
ren r?iwend int_enddate?
ren r??iwend int_enddate??

ren s?iwbeg sp_int_stdate?
ren s??iwbeg sp_int_stdate??
ren s?iwend sp_int_enddate?
ren s??iwend sp_int_enddate??

ren r?wthh hweight?
ren r??wthh hweight??
ren r?wtresp pweight?
ren r??wtresp pweight??
ren s?wtresp sp_pweight?
ren s??wtresp sp_pweight??

ren r?mstat marit?
ren r??mstat marit??

ren raracem race
ren s?racem sp_race?
ren s??racem sp_race??
ren rahispan hispanic
ren s?hispan sp_hispanic?
ren s??hispan sp_hispanic??

ren r?jcocc occupation?
ren r??jcocc occupation??

ren s?jcocc sp_occupation?
ren s??jcocc sp_occupation??

ren raedyrs educ_years
ren s?edyrs sp_educ_years?
ren s??edyrs sp_educ_years??

ren raedegrm educ
ren s?edegrm sp_educ?
ren s??edegrm sp_educ??

ren r?wgihr wage?
ren r??wgihr wage??
ren s?wgihr sp_wage?
ren s??wgihr sp_wage??

ren r?iearn ly?
ren r??iearn ly??
ren s?iearn sp_ly?
ren s??iearn sp_ly??

ren r?work work?
ren r??work work??
ren s?work sp_work?
ren s??work sp_work??

ren r?lbrf empstat?
ren r??lbrf empstat??
ren s?lbrf sp_empstat?
ren s??lbrf sp_empstat??

ren r?slfemp selfemp?
ren r??slfemp selfemp??
ren s?slfemp sp_selfemp?
ren s??slfemp sp_selfemp??

foreach v in walkr dress bath eat bed toilt {
ren r?`v' functional_`v'?
ren r??`v' functional_`v'??
ren s?`v' spfunctional_`v'?
ren s??`v' spfunctional_`v'??
}


ren r?hlthlm disab?
ren r??hlthlm disab??
ren s?hlthlm sp_disab?
ren s??hlthlm sp_disab??

ren r?back health_backprob?
ren r??back health_backprob??
ren s?back sphealth_backprob?
ren s??back sphealth_backprob??

foreach var in hibp diab cancr lung heart strok psych arthr {
ren r?`var' health_`var'?
ren r??`var' health_`var'??
ren s?`var' sphealth_`var'?
ren s??`var' sphealth_`var'??
}
*irregular health vars: memry alzhe demen ulcer 


ren ragender sex
ren s?gender sp_sex?
ren s??gender sp_sex??

ren rahrsamp  sample
ren s?hrsamp sp_sample?
ren s??hrsamp sp_sample??

ren r?isdi tr_ssdi?
ren r??isdi tr_ssdi??
ren r?issi tr_ssi?
ren r??issi tr_ssi??

ren h?atotn wealth_nonh?
ren h??atotn wealth_nonh??

ren h?absns wealth_bus?
ren h??absns wealth_bus??

ren h?atotb wealth_tot?
ren h??atotb wealth_tot??

ren r?issdi tr_di?
ren r??issdi tr_di??
ren s?issdi sp_tr_di?
ren s??issdi sp_tr_di??

ren r?ifssdi tr_dif?
ren r??ifssdi tr_dif??

ren s?ifssdi sp_tr_dif?
ren s??ifssdi sp_tr_dif??


#delimit ;
keep
id cohort byear bmonth bdate dyear dmonth ddate educ_years educ race hispanic sex veteran
sample
hhid numkids* homevalue*
inw*  sp_id* response* sp_response* ncore* couple* sp_cohort* sp_byear* sp_bmonth*
sp_bdate* sp_dyear* sp_dmonth* sp_ddate* agemonth* age* sp_agemonth* sp_age* int_stdate*
int_enddate* sp_int_stdate* sp_int_enddate* hweight* pweight* sp_pweight* marit*  
sp_race* sp_hispanic* 
sp_educ_years* sp_educ* occupation* sp_occupation*
wage* sp_wage* ly* sp_ly* 
wealth_nonh* wealth_bus* wealth_tot*
work* sp_work* empstat* sp_empstat* selfemp* sp_selfemp*
disab* sp_disab* health_* sphealth_* functional_* spfunctional_*
sp_sex* sp_sample*
tr_di* sp_tr_di* tr_ssdi* tr_ssi*

radnepi radapp* radrea* radrec* radend* radtype* radstat*
;

reshape long inw numkids sp_id response sp_response ncore couple sp_cohort sp_byear sp_bmonth
sp_bdate sp_dyear sp_dmonth sp_ddate agemonth age sp_agemonth sp_age int_stdate
int_enddate sp_int_stdate sp_int_enddate hweight pweight sp_pweight marit 
sp_race sp_hispanic homevalue
sp_educ_years sp_educ occupation sp_occupation
wage sp_wage ly sp_ly 
wealth_nonh wealth_bus wealth_tot
work sp_work empstat sp_empstat selfemp sp_selfemp
disab sp_disab health_backprob sphealth_backprob
health_hibp health_diab health_cancr health_lung health_heart health_strok health_psych health_arthr
sphealth_hibp sphealth_diab sphealth_cancr sphealth_lung sphealth_heart sphealth_strok sphealth_psych sphealth_arthr
functional_walkr functional_dress functional_bath functional_eat functional_bed functional_toilt
spfunctional_walkr spfunctional_dress spfunctional_bath spfunctional_eat spfunctional_bed spfunctional_toilt
sp_sex sp_sample
tr_di sp_tr_di tr_ssdi tr_ssi
tr_dif sp_tr_dif,
i(id) j(wave)
;
#delimit cr

gen month=month(int_stdate)
gen year=year(int_stdate)
bysort wave: egen year1 = mode(year)
drop year
rename year1 year

gen age1 = year - byear
drop age
ren age1 age

gen c = 1 if response==1| response==4
bysort year hhid: egen numadults=sum(c)
drop c

gen dead = (year >= dyear & response != 1 ) | (response==5 | response==6)
keep if response == 1 | dead==1


*keep the in-sample male in a couple:
keep if sex == 1 & cohort != 0
*if same-sex, keep the older one:
drop if sp_sex == 1 & age < sp_age

bysort id: egen everSSI=max(tr_ssi >0 & tr_ssi != . & tr_ssdi ==0)
bysort id: egen everworkhealthy=max(work == 1 & disab == 0 )

drop if everworkhealthy == 0 | everSSI == 1

replace disab = disab==1 
replace sp_disab = sp_disab==1
foreach var of varlist health* sphealth* {
replace `var'= `var'==1  | `var'==2 | `var' == 3
}
gen onDI = tr_di > 0 & !missing(tr_di)


*Deflating income measures:
tokenize 138300	138600	139100	139400	139700	140100	140500	140800	141100	141700	142100	142300	142800	143100	143300	143800	144200	144300	144500	144800	145000	145600	146000	146300	146300	146700	147100	147200	147500	147900	148400	149000	149300	149400	149800	150100	150500	150900	151200	151800	152100	152400	152600	152900	153100	153500	153700	153900	154700	155000	155500	156100	156400	156700	157000	157200	157700	158200	158700	159100	159400	159700	159800	159900	159900	160200	160400	160800	161200	161500	161700	161800	162000	162000	162000	162200	162600	162800	163200	163400	163500	163900	164100	164400	164700	164700	164800	165900	166000	166000	166700	167100	167800	168100	168400	168800	169300	170000	171000	170900	171200	172200	172700	172700	173600	173900	174200	174600	175600	176000	176100	176400	177300	177700	177400	177400	178100	177600	177500	177400	177700	178000	178500	179300	179500	179600	180000	180500	180800	181200	181500	181800	182600	183600	183900	183200	182900	183100	183700	184500	185100	184900	185000	185500	186300	186700	187100	187400	188200	188900	189100	189200	189800	190800	191700	191700	191600	192400	193100	193700	193600	193700	194900	196100	198800	199100	198100	198100	199300	199400	199700	200700	201300	201800	202900	203800	202800	201900	202000	203100	203437	204226	205288	205904	206755	207234	207603	207667	208547	209190	210834	211445	212174	212687	213448	213942	215208	217463	219016	218690	218877	216995	213153	211398	211933	212705	212495	212709	213022	214790	214726	215445	215861	216509	217234	217347	217488	217281	217353	217403	217290	217199	217605	217923	218275	219035	219590	220472	221187	221898	223046	224093	224806	224806	225395	226106	226597	226750	227169	227223	227842	228329	228807	229187	228713	228524	228590	229918	231015	231638	231249	231221	231679	232937	232282	231797	231893	232445	232900	233456	233544	233669	234100	234719	235288	235547	236028	236468	236918	237231	237498	237460	237477	237430	236983	236252	234747	235342	235976	236222	237001	237657	238034	238033	237498	237733	238017	237761	237652	237336	238080	238992	239557	240222	240101	240545	241176	241741	242026	242637	243618	244006	243892	244193	244004	244163	244243	245183	246435	246626	247284	247805	248859	249529	249577	250227	250792	251018	251214	251663	252182	252772	252594	252767


gen price = .
forvalues y = 1995(1)2013{
	forvalues m = 1(1)12{
		*normalizing by 1996 avg CPI
		replace price = `1'/156900 if year == `y' & month == `m' 
		macro shift
	}
}

foreach x in ly sp_ly wealth_nonh wealth_bus wealth_tot{
gen `x'_real = `x' / price
}


***EVENTS:
*two types:
*1) work limitation onset
*2) back problem onset

*restrict to people who:
*are not on DI before the event
*no more than HS education
*working age (age <= 62)
*are observed transitioning from healthy to sick (first time it happens)
*worked previously (NOT IMPLEMENTED HERE)


rename disab health_workprob
rename sp_disab sphealth_workprob

label define haspartner 0 "Unpartnered" 1 "Partnered"
label define applyvar 0 "Did Not Apply" 1 "Applied"
label define recvar 0 "No DI" 1 "Received DI"

gen wavestep=2
replace sp_ly = 0 if missing(sp_ly)
replace ly = 0 if missing(ly)
replace work = 0 if missing(work)
replace sp_work = 0 if missing(sp_work) 

local maxage 58
foreach disvar in health_workprob health_backprob {
preserve
ren `disvar' disab
ren sp`disvar' sp_disab

bysort id: egen everanydis=max((disab+sp_disab)>=1)
foreach y in year age {
bysort id: egen anyonset_`y' = min(`y'+ 99999*((disab+sp_disab)==0))
bysort id: egen refonset_`y' = min(`y'+ 99999*((disab)==0))
replace anyonset_`y'= . if anyonset_`y' >=99999
replace refonset_`y' = . if refonset_`y' >=99999

bysort id: egen first`y' = min(`y')
*dropping the guys for whom I don't observe the event
*replace anyonset_`y' = . if first`y'==anyonset_`y'
*you don't belong to cohort refonset if:
*1) household had another condition first
*2) we don't observe you transition from being healthy
replace refonset_`y' = . if anyonset_`y'<refonset_`y'
replace refonset_`y' = . if first`y'==refonset_`y'
}
sort id year
bysort id: gen diffyear = year - year[_n-1]
bysort id: gen basediffyear = year[_n+1] - year
bysort id: egen diffyear_onset = max(diffyear*(year==anyonset_year))
gen eventtype = 1 if refonset_year == anyonset_year & !missing(refonset_year) & diffyear_onset==2
drop  diffyear_onset

bysort id: egen prework = max((work==1 & year==anyonset_year-2))

*No disabled spouses before refonset_age:
*tab sp_disab if age<refonset_age & refonset_age != . & eventtype == 1
*103 simultaneous disabilities:
*tab sp_disab if age==refonset_age & refonset_age != . & eventtype == 1

*DI receipt:
*Note - I include DI receipt up to 2 years prior to health event because there are 2 years between each HRS wave
egen firstappyear = rowmin(radappy*)
gen firstapplyevent = firstappyear >= refonset_year -2 & firstappyear <= refonset_year+5 & !missing(refonset_year) & !missing(firstappyear)
*drop the guys who are on DI before the event
replace firstapplyevent = . if firstappyear < refonset_year-2 

egen firstrecyear = rowmin(radrecy*)
gen firstrecevent = firstrecyear >= refonset_year -2 & firstrecyear <= refonset_year+5 & !missing(refonset_year) & !missing(firstrecyear)
*drop the guys who are on DI before the event
replace firstrecevent = . if firstrecyear < refonset_year-2 | firstrecyear < firstappyear | firstappyear < refonset_year-2 


egen firstappealyear = rowmin(radreay*)
gen firstappealevent = !missing(firstappealyear) if firstapplyevent ==1 & (firstappealyear>=firstappyear | !missing(firstappealyear))

*drop people from denominator if they are currently on DI
foreach x of varlist radappy* radrecy*{
gen `x'inevent = `x'>= refonset_year -2 & `x' <= refonset_year +5 & !missing(refonset_year) & !missing(`x')
}

gen alreadyDI = 0
gen onDI_rec = 0
gen onDI_rec_ly = 0
gen applyDI_rec =0 
gen appealDI_rec =0
gen yetapplyDI_rec =0 
gen yetappealDI_rec =0
forvalues v = 1(1)11 {
replace alreadyDI = 1 if radrecy`v' < refonset_year - 2 & radendy`v' >= refonset_year  & !missing(refonset_year) & !missing(radendy`v')
gen allowDIinevent`v' = radappy`v'inevent==1 & radstat`v'==4
replace onDI_rec = 1 if radrecy`v' <= year & (radendy`v' >= year | missing(radendy`v')) 
replace onDI_rec_ly = 1 if radrecy`v' <= year - 1 & (radendy`v' >= year - 1 | missing(radendy`v')) 
replace applyDI_rec = 1 if radappy`v' == year |  radappy`v' == year -1
replace appealDI_rec = 1 if radreay`v' == year |  radreay`v' == year -1
replace yetapplyDI_rec = 1 if radappy`v' <= year 
replace yetappealDI_rec = 1 if radreay`v' <= year 
*& radrecy`v' > year-4
*set event missing if person applied and received DI well before suffering it
*replace refonset_year = . if radrecy`v' < refonset_year - 2 
*onDI_rec is missing if first receipt is before first application
*replace onDI_rec = . if radrecy`v' < refonset_year - 2 | firstrecyear < firstappyear | firstappyear < refonset_year-2
}
bysort id: egen getsDI= max(onDI_rec) if year <= refonset_year+5
sort id year
bysort id: gen yetDI = sum(onDI_rec) 
replace yetDI = yetDI > 0 & yetDI != .
*bysort id: egen anyonDI = max(onDI_rec*(year>=refonset_year -2 & year <= refonset_year+4))

tab yetDI firstrecevent if year == refonset_year +4

egen anyappevent = rowmax(radappy*inevent)
egen anyrecevent = rowmax(radrecy*inevent)
egen anyrecevent2=rowmax(allowDIinevent*)
*anyrecevent2 one based on allowance status and application date misses 
*more people than it brings in (who have missing receipt date data).

replace anyappevent = . if alreadyDI==1
replace anyrecevent = . if alreadyDI==1
replace anyrecevent2 = . if alreadyDI==1


*singles are more likely to initiate an application after disability, but success rate is lower:
tab  marit firstapplyevent if year==refonset_year & eventtype==1 & educ <=3 & refonset_age <=`maxage' & prework==1, row
tab  marit firstappealevent   if year==refonset_year & eventtype==1 & educ <=3 & refonset_age <=`maxage' & prework==1, row
tab  marit firstrecevent   if year==refonset_year & eventtype==1 & educ <=3 & refonset_age <=`maxage' & prework==1, row
tab  marit anyappevent if year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage' & prework==1, row
tab  marit anyrecevent if year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage' & prework==1, row


gen haspartner = marit >=1 & marit <=3
label values haspartner haspartner

*being on DI before a condition is rare, same for partnered and unpartnered
tab onDI haspartner if year < anyonset_year & !missing(anyonset_year) & educ <=3  & refonset_age <=`maxage' & prework==1, col

*getsDI approximates firstrecevent:
tab haspartner getsDI  if year == refonset_year & eventtype==1  & educ <=3  & refonset_age <=`maxage' & prework==1, row

*unpartnered are more likely to apply, less likely to make it in:
tab  haspartner firstapplyevent   if year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage' & prework==1, row
tab  haspartner firstappealevent   if year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage'& prework==1, row
tab  haspartner firstrecevent   if year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage'& prework==1 , row
tab  haspartner firstrecevent   if year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage'& prework==1 & !missing(firstappyear), row
tab  haspartner anyappevent if year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage'& prework==1, row
tab  haspartner  anyrecevent if year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage'& prework==1, row

mean onDI_rec if age>=refonset_age &eventtype==1  & refonset_age != . & educ <= 3 & prework==1 & refonset_age <= `maxage' & eventtype==1, over(haspartner)

bysort haspartner: tab firstapplyevent firstrecevent  if year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage'& prework==1, row
bysort haspartner: tab anyappevent anyrecevent  if year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage'& prework==1, row

bysort id: egen haspartner_event = max(haspartner*(year==refonset_year))
bysort id: egen temp= max(year==refonset_year)
replace haspartner_event = . if temp == 0
drop temp

tab  haspartner_event firstrecevent   if year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage'& prework==1 , row
tab  haspartner_event yetDI if year==refonset_year+4 & eventtype==1 & educ <=3  & refonset_age <=`maxage'& prework==1 , row

gen postobs = year >= refonset_year & refonset_year != .
bysort id: egen numobs = sum(postobs)
drop postobs
bysort haspartner : sum  numobs if year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage'& prework==1 
tab numobs if haspartner_event==0 & year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage'& prework==1
tab numobs if haspartner_event==1 & year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage'& prework==1

*********************************************
*difference in time from apply to allowance:
*********************************************
*unpartnered people take LONGER to get onto DI...
gen wait_time = .
egen firstappdate = rowmin(radappd*)
egen firstrecdate = rowmin(radrecd*)

gen everappdate = .
gen everrecdate = .
bysort id: egen haspartner_apply = max(haspartner*(year==firstappyear | year == firstappyear-1))
bysort id: egen temp= max(year==firstappyear | year == firstappyear-1)
replace haspartner_apply = . if temp == 0
drop temp

forvalues v = 1(1)11 {
*if the year variable is missing, the date variable is imputed (I believe)
replace wait_time = radrecd`v' - radappd`v'  if wait_time ==. & !missing(radrecy`v')  & (radappy`v'==firstappyear)
replace everappdate = radappd`v' if (radappy`v' >= refonset_year - 2 & radappy`v' <= refonset_year +5) & (radappd`v' < everappdate | missing(everappdate)) & (!missing(refonset_year) &  !missing(radappy`v'))
replace everrecdate = radrecd`v' if (radrecy`v' >= refonset_year - 2 & radrecy`v' <= refonset_year +5) & (radrecd`v' < everrecdate | missing(everrecdate)) & (!missing(refonset_year) &  !missing(radrecy`v'))
}
*some people say they get on DI BEFORE applying:
replace wait_time = . if wait_time < 0


gen firstdiff = firstrecdate - firstappdate
gen everdiff = everrecdate - everappdate

bysort haspartner: sum firstdiff if year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage' & firstdiff >=0
bysort haspartner: sum everdiff  if year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage' & everdiff >=0

label var firstapplyevent applyvar
label var firstrecevent recvar
label var anyappevent applyvar
label var anyrecevent recvar

gen nopartner = haspartner == 0

reg firstapplyevent nopartner haspartner if  year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage' & prework==1, cluster(id) nocons
eststo app
reg firstappealevent nopartner haspartner if firstapplyevent==1 & year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage' & prework==1, cluster(id) nocons
eststo appeal
reg firstrecevent nopartner haspartner if firstapplyevent==1 & year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage' & prework==1, cluster(id) nocons
eststo rec

reg firstapplyevent haspartner if  year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage' & prework==1, cluster(id) 
eststo appdiff
reg firstappealevent haspartner if  firstapplyevent==1 & year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage' & prework==1, cluster(id) 
eststo appealdiff
reg firstrecevent haspartner if  firstapplyevent==1 & year==refonset_year & eventtype==1 & educ <=3  & refonset_age <=`maxage' & prework==1, cluster(id) 
eststo recdiff


esttab app rec using "./HRS_figures/firstapp_`disvar'.tex", nonotes nonum coeflabels(nopartner "Unpartnered" haspartner "Partnered") mtitles("Share who Applied for DI" "Share of Applicants Who Received DI") se noobs tex fragment  replace  nogaps
esttab appdiff recdiff using "./HRS_figures/firstapp_`disvar'.tex", nonotes nonum keep(haspartner) coeflabels(haspartner "Difference") nomtitles se tex fragment  append nogaps lines

egen everself=sum(self==1),by(id)	

keep if educ <= 3  & age <= `maxage'+6 & everself== 0

********************************************************************************
*************SAMPLE STATISTICS****:
ren  disab `disvar'
ren  sp_disab sp`disvar'

gen c =1
bysort id haspartner: egen nwaves=sum(c)
bysort year haspartner: egen npeople=sum(c)
drop c
gen weighttemp = 1/npeople
gen weighttemp2 = 1/nwaves

gen hsgrad = educ != 0
gen white = race==1



local statvars age hsgrad white numadults numkids health_* npeople nwaves 
local healthvars  health_hibp health_backprob health_diab health_cancr health_lung health_heart health_strok health_psych health_arthr health_workprob

local agelab "Age"
local hsgradlab "Has HS Degree"
local whitelab "White"
local numadultslab "\# Adults in Household"
loca numkidslab "\# Living Children"
local npeoplelab "\# Households/Wave"
local nwaveslab "\# Waves/Household"
local health_hibplab "Has Hypertension"
local health_backproblab "Has Back Problem"
local health_diablab "Has Diabetes"
local health_cancrlab "Has Cancer"
local health_lunglab "Has Lung Disease"
local health_heartlab "Has Heart Problem"
local health_stroklab "Had a Stroke"
local health_psychlab "Has Psych. Problem"
local health_arthrlab "Has Arthritis"
local health_workproblab "Has Work Limitation"

foreach stvar of varlist `statvars' {
if("`stvar'"!="npeople" & "`stvar'"!="nwaves") local weight 
if ("`stvar'" == "npeople") local weight [aw=weighttemp]
if ("`stvar'" == "nwaves") local weight [aw=weighttemp2]
reg `stvar' `weight' if haspartner==1, cluster(id)
nlcom (_b[_cons]), post
eststo stat_married
reg `stvar' `weight' if haspartner==0, cluster(id)
nlcom (_b[_cons]), post
eststo stat_single
reg `stvar' haspartner `weight' , cluster(id)
nlcom (_b[haspartner]), post
eststo stat_diff


if("`stvar'"=="age"){
 local append replace
 local mtitles mtitles("Single" "Partnered" "Partnered-Single")
 }

else {
local append append
local mtitles nomtitles nolines
}
if("`stvar'"=="npeople" ) esttab stat_single stat_married using "./HRS_figures/statsbymar`disvar'.tex", nonotes nonum coeflabels(_nl_1 "``stvar'lab'")  cells(b) nomtitles collabels(,none)  noobs tex fragment  `append'  nogaps star(* 0.10 ** 0.05 *** 0.01)
else if("`stvar'"=="nwaves") esttab stat_single stat_married using "./HRS_figures/statsbymar`disvar'.tex", nonotes nonum coeflabels(_nl_1 "``stvar'lab'")  cells(b) nomtitles nolines collabels(,none)  noobs tex fragment  `append'  nogaps star(* 0.10 ** 0.05 *** 0.01)
else if ("`stvar'" == "health_backprob") esttab stat_single stat_married stat_diff using "./HRS_figures/statsbymar`disvar'.tex", nonotes nonum coeflabels(_nl_1 "``stvar'lab'")  nomtitles se noobs tex fragment  `append'  nogaps star(* 0.10 ** 0.05 *** 0.01)
else esttab stat_single stat_married stat_diff using "./HRS_figures/statsbymar`disvar'.tex", nonotes nonum coeflabels(_nl_1 "``stvar'lab'")  `mtitles' se noobs tex fragment  `append'  nogaps star(* 0.10 ** 0.05 *** 0.01)
}
estimates drop _all

foreach stvar of local healthvars {
if("`stvar"=="`disvar'") continue
local weight 
reg `stvar' `weight' if `disvar'==0, cluster(id)
nlcom (_b[_cons]), post
eststo stat_healthy
reg `stvar' `weight' if `disvar'==1, cluster(id)
nlcom (_b[_cons]), post
eststo stat_mod
reg `stvar' `disvar' `weight' if `disvar' == 0 | `disvar' == 1 , cluster(id)
nlcom (_b[`disvar']), post
eststo stat_diffmod


if("`stvar'"=="health_hibp"){
 local append replace
 local mtitles mtitles("Healthy" "Disabled" "Disabled - Healthy")
 }

else {
local append append
local mtitles nomtitles nolines
}
if("`stvar'"=="npeople" ) esttab stat_healthy stat_mod using "./HRS_figures/statsbyhealth`event'`sample'.tex", nonotes nonum coeflabels(_nl_1 "``stvar'lab'")  cells(b) nomtitles collabels(,none)  noobs tex fragment  `append'  nogaps star(* 0.10 ** 0.05 *** 0.01)
else if("`stvar'"=="nwaves") esttab stat_healthy stat_mod using "./HRS_figures/statsbyhealth`event'`sample'.tex", nonotes nonum coeflabels(_nl_1 "``stvar'lab'")  cells(b) nomtitles nolines collabels(,none)  noobs tex fragment  `append' nogaps star(* 0.10 ** 0.05 *** 0.01)
else esttab stat_healthy stat_mod stat_diffmod using "./HRS_figures/statsbyhealth`disvar'.tex", nonotes nonum coeflabels(_nl_1 "``stvar'lab'")  `mtitles' se noobs tex fragment  `append'  nogaps star(* 0.10 ** 0.05 *** 0.01)
}
estimates drop _all

drop  weighttemp weighttemp2
estimates drop _all

ren `disvar' disab
ren sp`disvar' sp_disab
********************************************************************************
**Current stocks, conditional on having been allowed on DI as an adult:
gen married = marit == 1 | marit == 2
forvalues m = 0(1)1{
forvalues di = 0(1)1{
sum onDI_rec   if age >= 50 & firstrecyear  - byear>= 23 & !missing(firstrecyear) & age <= 62 & disab == `di' & married == `m'
}
}



*killing people disabled older than max age.
*can still be used as controls--but wont be treated
*replace refonset_age = . if refonset_age > `maxage'
*drop the guys who interact with DI before the event
replace refonset_age = . if prework!=1 | firstrecyear < refonset_year-2 |  firstappyear < refonset_year-2 | refonset_age >`maxage'
replace refonset_year = . if refonset_age == . 

/*
*singles who get on after the event, get on faster!!
mean  wait_time  if year==refonset_year - 2 & eventtype==1 & refonset_age <=`maxage' & prework==1, over(haspartner)
test _b[wait_time:Unpartnered] = _b[wait_time:Partnered]
*/
*true in general!!
*this one is garbage
*mean  wait_time if refonset_age <=`maxage' , over(haspartner)
*test _b[wait_time:Unpartnered] = _b[wait_time:Partnered]
save "hrs_`disvar'", replace
restore
}

